package com.arvato.common;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List;

import com.arvato.wxwork.bean.bean_do.staff.WxworkSysBaDo;



/**
 ** CSV操作(读取和写入)   * @author ding
 ** 
 * @version 2018-04-23   
 */
public class CSVUtils {

	public static List<String> importCsv(File file) {
		List<String> dataList = new ArrayList<String>();
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
			BufferedReader br = new BufferedReader(isr);
			String line = "";
			while ((line = br.readLine()) != null) {
				dataList.add(line);
			}
			br.close();
			br = null;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dataList;
	}

	public static void main(String[] args) throws Exception {
		   List<String> str=convert(new File("D:\\zipfile\\Seibel - Miracle\\\\BA-Siebel-La Roche Posay-20160623.csv"));
		   for(int i=1;i<str.size();i++) {
			   String arg[]=str.get(i).split(";");
			   WxworkSysBaDo ba=new WxworkSysBaDo();
			   ba.setCountry(arg[0]);
			   ba.setOrganization(arg[1]);
			   ba.setBaId(arg[2]);
			   ba.setIdentSource(arg[3]);
			   ba.setCounterId(arg[4]);
			   ba.setLastName(arg[5]);
			   ba.setFirstName(arg[6]);
			   ba.setPositionType(arg[7]);
			   ba.setStartDt(arg[8]);
			   ba.setEndDt(arg[9]);
			   ba.setSsn(arg[10]);
			   ba.setPhone(arg[11]);
			   ba.setMobile(arg[12]);
			   ba.setEmail(arg[13]);
			   ba.setAddress(arg[14]);
			   ba.setAddress2(arg[15]);
			   ba.setProvince(arg[16]);
			   ba.setCity(arg[17]);
			   ba.setZipcode(arg[18]);
           }
	}
	  public static String sourceCharset = "unicode"; // 源文件编码
	  public static String targetCharset = "utf8"; // 目标文件编码
	  public static List<String> convert(File file) throws IOException {
		  StringBuffer sb = new StringBuffer();
			List<String> dataList = new ArrayList<String>();
	        // 如果是文件则进行编码转换，写入覆盖原文件
	        if (file.isFile()) {
	            InputStreamReader isr = new InputStreamReader(new FileInputStream(
	                    file), sourceCharset);
	            BufferedReader br = new BufferedReader(isr);
	          
	            String line = null;
	            while ((line = br.readLine()) != null) {
	                // 注意写入换行符
	                sb.append(line + "\n");
	                dataList.add(line);
	            }
	            br.close();
	            isr.close();
	        } else {
	            for (File subFile : file.listFiles()) {
	                convert(subFile);
	            }
	        }
	        return dataList;
	    }

	// Unicode转中文
	public static String decodeUnicode(final String dataStr) {
		int start = 0;
		int end = 0;
		final StringBuffer buffer = new StringBuffer();
		while (start > -1) {
			end = dataStr.indexOf("\\u", start + 2);
			String charStr = "";
			if (end == -1) {
				charStr = dataStr.substring(start + 2, dataStr.length());
			} else {
				charStr = dataStr.substring(start + 2, end);
			}
			char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
			buffer.append(new Character(letter).toString());
			start = end;
		}
		return buffer.toString();
	}

}
